// BlogLikeService.java
package net.web.service;

import net.web.dao.BlogLikeDao;
import net.web.entity.BlogLike;
import net.web.entity.MessageNotification;
import net.web.service.MessageNotificationService;
import net.web.dao.BlogDao;

public class BlogLikeService {
    private BlogLikeDao likeDao = new BlogLikeDao();
    private MessageNotificationService messageNotificationService = new MessageNotificationService();
    private BlogDao blogDao = new BlogDao();

    // 在 BlogLikeService 的 likeBlog 方法中添加消息通知逻辑
    public boolean likeBlog(int blogId, int userId) {
        if (!likeDao.isLiked(blogId, userId)) {
            BlogLike blogLike = new BlogLike();
            blogLike.setBlogId(blogId);
            blogLike.setUserId(userId);
            boolean result = likeDao.insert(blogLike);
            if (result) {
                likeDao.updateLikeCount(blogId, 1);
                Integer blogAuthorId = blogDao.getAuthorIdByBlogId(blogId);
                if (blogAuthorId != null && blogAuthorId != userId) {
                    MessageNotification notification = new MessageNotification();
                    notification.setRecipientId(blogAuthorId);
                    notification.setSenderId(userId);
                    notification.setBlogId(blogId);
                    notification.setMessageType("like");
                    messageNotificationService.sendNotification(notification);
                }
            }
            return result;
        }
        return false;
    }

    public boolean unlikeBlog(int blogId, int userId) {
        boolean result = likeDao.delete(blogId, userId);
        if (result) {
            likeDao.updateLikeCount(blogId, -1); // 取消点赞-1
        }
        return result;
    }

    public boolean isLiked(int blogId, int userId) {
        return likeDao.isLiked(blogId, userId);
    }

    public int getLikeCount(int blogId) {
        return likeDao.getLikeCount(blogId);
    }
}